package com.firefly.admin.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.annotations.annotation.OperationLog;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.firefly.admin.system.entity.FySystemRole;
import com.firefly.admin.system.service.FySystemRoleService;
import com.firefly.admin.system.vo.SystemRoleAssignMenusVo;
import com.firefly.admin.system.vo.SystemRoleQueryVo;
import com.firefly.admin.system.vo.SystemRoleStatusUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统角色接口API")
@RestController
@RequestMapping("/system/role")
public class FySystemRoleController {
    @Autowired
    private FySystemRoleService fySystemRoleService;

    /**
     * 分页查询系统角色接口
     *
     * @param systemRoleQueryVo 分页查询参数
     * @return 分页列表
     */
    @ApiOperation(value = "分页查询系统角色接口", notes = "分页查询系统角色接口", httpMethod = "GET")
    @PreAuthorize(value = "hasAuthority('system:role')")
    @GetMapping("/list")
    public Result<Page<FySystemRole>> getSystemRoleList(SystemRoleQueryVo systemRoleQueryVo) {
        return fySystemRoleService.getSystemRoleList(systemRoleQueryVo);
    }

    /**
     * 获取可用角色列表接口
     *
     * @return 角色列表
     */
    @ApiOperation(value = "获取可用角色列表接口", notes = "获取可用角色列表接口", httpMethod = "GET")
    @GetMapping("/usable/list")
    public Result<List<FySystemRole>> getAllUsableRoleList() {
        return fySystemRoleService.getAllUsableRoleList();
    }

    /**
     * 获取系统角色信息接口
     *
     * @param roleId 角色ID
     * @return 角色信息
     */
    @ApiOperation(value = "获取系统角色信息接口", notes = "通过角色ID获取角色详细信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", required = true, value = "角色ID", paramType = "path", dataType = "Long")
    })
    @GetMapping("/{roleId}")
    public Result<FySystemRole> getSystemRole(@PathVariable Long roleId) {
        return fySystemRoleService.getSystemRole(roleId);
    }

    /**
     * 系统角色信息新增接口
     *
     * @param fySystemRole 角色信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统角色信息新增接口", notes = "系统角色信息新增接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ROLE, operateDesc = "系统角色新增")
    @PreAuthorize(value = "hasAuthority('system:role:add')")
    @PostMapping("/add")
    public Result<FySystemRole> add(@RequestBody FySystemRole fySystemRole) {
        return fySystemRoleService.add(fySystemRole);
    }

    /**
     * 系统角色信息编辑接口
     *
     * @param fySystemRole 角色信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统角色信息编辑接口", notes = "系统角色信息编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ROLE, operateDesc = "系统角色编辑")
    @PreAuthorize(value = "hasAuthority('system:role:update')")
    @PostMapping("/update")
    public Result<FySystemRole> update(@RequestBody FySystemRole fySystemRole) {
        return fySystemRoleService.update(fySystemRole);
    }

    /**
     * 系统角色状态编辑接口
     *
     * @param systemRoleStatusUpdateVo 状态信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统角色状态编辑接口", notes = "系统角色状态编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ROLE, operateDesc = "系统角色修改状态")
    @PreAuthorize(value = "hasAuthority('system:role:update:status')")
    @PostMapping("/update/status")
    public Result<FySystemRole> updateStatus(@RequestBody SystemRoleStatusUpdateVo systemRoleStatusUpdateVo) {
        return fySystemRoleService.updateStatus(systemRoleStatusUpdateVo);
    }

    /**
     * 系统角色信息删除接口
     *
     * @param roleId 角色ID
     * @return 操作结果
     */
    @ApiOperation(value = "系统角色信息删除接口", notes = "系统角色信息删除接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ROLE, operateDesc = "系统角色删除")
    @PreAuthorize(value = "hasAuthority('system:role:delete')")
    @PostMapping("/delete")
    public Result<String> delete(Long roleId) {
        return fySystemRoleService.delete(roleId);
    }

    /**
     * 系统角色分配菜单权限接口
     *
     * @param systemRoleAssignMenusVo 角色权限信息
     * @return 操作结果
     */
    @ApiOperation(value = "系统角色分配菜单权限接口", notes = "系统角色分配菜单权限接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_ROLE, operateDesc = "系统角色分配菜单权限")
    @PreAuthorize(value = "hasAuthority('system:role:assign:menus')")
    @PostMapping("/assign/menus")
    public Result<String> assignMenus(@RequestBody SystemRoleAssignMenusVo systemRoleAssignMenusVo) {
        return fySystemRoleService.assignMenus(systemRoleAssignMenusVo);
    }

    /**
     * 系统角色名称校验接口
     *
     * @param roleName 角色名称
     * @param roleId   角色ID
     * @return 校验结果
     */
    @ApiOperation(value = "系统角色名称校验接口", notes = "系统角色名称校验接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", required = true, value = "角色名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "roleId", required = true, value = "角色ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/name/check")
    public Result<String> roleNameCheck(String roleName, Long roleId) {
        return fySystemRoleService.roleNameCheck(roleName, roleId);
    }

    /**
     * 通过角色ID获取菜单接口
     *
     * @param roleId 角色ID
     * @return 查询结果
     */
    @ApiOperation(value = "通过角色ID获取菜单接口", notes = "通过角色ID获取菜单接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", required = true, value = "角色ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/menu")
    public Result<List<Long>> getRoleMenus(Long roleId) {
        return fySystemRoleService.getRoleMenus(roleId);
    }
}
